<template>
	<view>
		<view class="wrap">
			<view class="order" v-for="(item,index) in WaitCommentData" :key="index">
				<u-skeleton rows="6" :loading="dataLoading">
					<view @click="toDetails(item.goodsId)">
						<view class="top">
							<view class="left">
								<view class="store">{{item.storeName}}</view>
								<u-icon name="arrow-right" color="rgb(203,203,203)" :size="24"></u-icon>
							</view>
							<view class="right">待评价</view>
						</view>
						<view class="item">
							<view class="left">
								<image :src="item.goodsImage" mode="aspectFill"></image>
							</view>
							<view class="content">
								<view class="title u-line-2">{{item.goodsName}}</view>
								<view class="type">已售：{{item.sale}}</view>
								<view class="delivery-time">商品怎么样，给个评价吧！</view>
							</view>
							<view class="right">
								<view class="price">
									￥{{toFixedOneDecimal(item.totalPrice)}}
									<text class="decimal">{{toFixedOneDecimal(item.price)}}</text>
								</view>
								<view class="number">x{{item.number}}</view>
							</view>
						</view>
						<view class="total">
							<view style="margin-top: 5px;">
								订单号：{{item.orderId}}
							</view>
							<view>
								共{{item.number}}件商品 合计:
								<text class=" total-price">
									<text class="decimal">￥{{toFixedOneDecimal(item.totalPrice)}}</text>
								</text>
							</view>
						</view>
					</view>
					<view class="right_pay">
						<view @click="handleDoComment(item.goodsId,item.orderId)" class="cart btn u-line-1">评价</view>
					</view>
				</u-skeleton>
			</view>

			<!-- 空状态 -->
			<view v-if="isEmpty">
				<u-empty mode="order" icon="http://cdn.uviewui.com/uview/empty/car.png">
				</u-empty>
			</view>

			<!-- 评价弹框 -->
			<u-popup :round="10" :overlayOpacity="0.3" :show="show" mode="center" @close="close">
				<view class="card_comment">
					<u--form labelPosition="left" :model="comments" :rules="rules" ref="commentForm">
						<u-form-item prop="comment" ref="comment">
							<u--textarea v-model="comments.comment" placeholder="请输入内容" count></u--textarea>
						</u-form-item>
						<u-form-item prop="score" ref="score">
							<u-rate activeColor="#ff9900" :size="26" :allowHalf="true"
								v-model="comments.score"></u-rate>
						</u-form-item>
					</u--form>
					<view style="display: flex;padding: 10px;justify-content: flex-end;">
						<view style="width: 60px;">
							<u-button type="primary" @click="doSubmit" text="提交"></u-button>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		getUserInfo
	} from "@/stores/index.js";
	import {
		queryWaitComment
	} from "@/api/usergoodslog/index.js";
	import {
		saveGoodsComment
	} from "@/api/comment/index.js";

	const user = getUserInfo();
	const isEmpty = ref(false);
	const dataLoading = ref(true);
	onLoad((options) => {
		getWaitComment();
	})

	const WaitCommentData = ref([]);
	//获取当前用户的待付款订单
	const getWaitComment = () => {
		queryWaitComment(user.id).then((res) => {
			if (res.code === 200 && res.data !== null) {
				WaitCommentData.value = res.data;
				dataLoading.value = false;
			} else {
				isEmpty.value = true;
				dataLoading.value = false;
			}
		})
	}

	//表单校验
	const commentForm = ref();
	const rules = ref({
		comment: [{
			required: true,
			message: '请输入评价内容',
			trigger: ['blur', 'change'],
		}, ],
		score: [{
			required: true,
			message: '请选择评分',
			trigger: ['blur', 'change'],
		}, ],
	})

	const show = ref(false);
	const comments = ref({
		orderId: null,
		userId: null,
		goodsId: null,
		comment: "",
		score: null
	})
	//评价方法
	const handleDoComment = (goodsId, orderId) => {
		show.value = true;
		comments.value.goodsId = goodsId;
		comments.value.orderId = orderId;
	}

	//提交评价
	const doSubmit = () => {
		comments.value.userId = user.id;
		if ((comments.value.comment !== '') && (comments.value.score !== null)) {
			saveGoodsComment(comments.value)
				.then((res) => {
					if (res.code === 200) {
						uni.showToast({
							title: "感谢评价！"
						})
						show.value = false;
						comments.value = {};
						uni.redirectTo({
							url: "/pages/my/waitcomment/waitcomment"
						})
					}
				})
		}
	}

	function close() {
		show.value = false;
		comments.value = {};
	}

	//金额小数点
	function toFixedOneDecimal(num) {
		return parseFloat(num.toFixed(1));
	}

	//跳往商品详情页
	const toDetails = (goodsId) => {
		uni.navigateTo({
			url: "/pages/detail/detail?goodsId=" + goodsId
		})
	}
</script>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;
			display: flex;
			justify-content: space-between;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-info-dark;
			}

			.evaluate {
				color: $u-warning-dark;
				border-color: $u-warning-dark;
			}
		}
	}

	.right_pay {
		display: flex;
		font-size: 28rpx;
		justify-content: flex-end;
		align-items: center;
		margin-top: 15px;

		.btn {
			line-height: 66rpx;
			padding: 0 30rpx;
			border-radius: 36rpx;
			color: #ffffff;
		}

		.cart {
			background-color: #2979ff;
		}

		.cart:active {
			background-color: #a0cfff;
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: auto;
		width: 100%;
		background-color: #F2F6FC;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}

	.card_comment {
		background-color: #fff;
		box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
		width: 300px;
		padding: 10px;
		border-radius: 10px;
		flex-direction: column;
		transition: all 0.3s ease;
	}
</style>